import axios from 'axios'
import router from '../router/index.js'
import { Message } from 'element-ui'


let base = process.env.API_HOST;
// let base = '/DeptFileSite';  //正式使用
//let base = '/api';  //测试使用
//响应拦截
axios.interceptors.response
  .use(
    success => {
      console.log('拦截器test');
      if (success.status && success.status == 200 && success.data.status == 500) {
        console.log('success', success)
        return;
      }
      return success;
    },
    error => {
      console.log('error', error.response)
      if (error.response.status == 504 || error.response.status == 404) {
        Message.error({message: '服务器连接失败!'});
      } else if (error.response.status == 403) {
        //资源不可用或没有权限
        Message.error({ message: '尚无权限!' });
      } else if (error.response.status == 401) {
        //未登录
        Message.error({ message: '连接已断开，请重新登录!' });
        // router.replace('/');
      } else {

      }
      return error;
    })
export const postRequest = (url, params) => {
  return axios({
    method: 'post',
    url: `${base}${url}`,
    data: params,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    transformRequest: [function (data) {
      let ret = ''
      for (let it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    withCredentials: true

  });
}
export const exportRequest = (url, params) => {
  return axios({
    method: 'post',
    url: `${base}${url}`,
    responseType: 'blob',
    data: params,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    transformRequest: [function (data) {
      let ret = ''
      for (let it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    withCredentials: true
  })
}
export const uploadFileRequest = (url, params, callback) => {
  return axios({
    method: 'post',
    url: `${base}${url}`,
    data: params,
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    onUploadProgress: function (progressEvent) {
      let percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
      callback(percentCompleted);
    },
    withCredentials: true
  });
}
export const downloadRequest = (url, params) => {
  return axios({
    method: 'post',
    url: `${base}${url}`,
    data: params,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    responseType: 'blob',
    transformRequest: [function (data) {
      let ret = ''
      for (let it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    withCredentials: true
  });
}

export const putRequest = (url, params) => {
  return axios({
    method: 'put',
    url: `${base}${url}`,
    data: params,
    /*transformRequest: [function (data) {
      let ret = ''
      for (let it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],*/
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    },
    withCredentials: true
  });
}

export const getRequest = (url, params) => {
  return axios({
    method: 'get',
    params: params,
    transformRequest: [function (params) {
      let ret = ''
      for (let it in params) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    url: `${base}${url}`,
    withCredentials: true
  });
}



export {
  base
}
